clear all
set more off

cd "$path"

/***** COORDINATION GRAPHS ***/
use "Data/proprietary/01 Master Data", replace
drop if year==2010
gen chara1=nvisible1+ninvisible1
gen chara2=nvisible2+ninvisible2

bysort idprov: egen mattempted=mean(attempted1)
su mattempted, de
local med=r(p50)
su year attempted1 guardia1 chara1
su year attempted1 guardia1 chara1 if mattempted<=`med'
su year attempted1 guardia1 chara1 if mattempted>`med'

tempfile temp
tempfile tempmerge
save `temp', replace

local tot=1

rename chiave_agenzia1 nid
rename guardia1 guardmerge
forval i=1/`tot' {
 gen nid`i'=nid
}
drop nid
keep nid* year guardmerge
sort nid1 year
save `tempmerge', replace

forval y=2000/2009 {
 use `tempmerge', clear
 tempfile tempmerge`y'
 keep if year==`y'
 save `tempmerge`y'', replace

 use `temp', clear
 tempfile temp`y'
 keep if year==`y'
 save `temp`y'', replace
 use `temp`y'', clear
 keep if year==`y'
 geonear chiave_agenzia1 longit lati using `temp`y'', n(chiave_agenzia1 longit lati) ignoreself nearcount(`tot') wide
 if `tot'==1 {
  rename nid nid1
 }
 save `temp`y'', replace
 
 forval i=1/`tot' {
  use `tempmerge`y'', clear
  sort nid`i'
  save `tempmerge`y'', replace
  use `temp`y'', clear
  sort nid`i' 
  merge nid`i' using `tempmerge`y''
  drop if _merge==2
  drop _merge
  rename guardmerge guardmerge`i'
  save `temp`y'', replace
 }
 save `temp`y'', replace
}

clear
forval y=2000/2009 {
 append using `temp`y''
}  

*egen n_comune=count(guardia1), by(year idcomune)
*drop if n_comune<20

 
egen p_italy=mean(guardia1), by(year)
*egen p_provincia=mean(guardia1), by(year idprovincia)
*egen p_comune=mean(guardia1), by(year idcomune)

forval n=1/`tot' { 
 foreach x in "same" "guards" "noguards" "different" {
  gen `x'_`n'=0
 }
 forval i=1/`n' {
  replace same_`n'=same_`n'+(guardia1==guardmerge`i')
  replace guards_`n'=guards_`n'+(guardia1==guardmerge`i')*(guardia1==1)
  replace noguards_`n'=noguards_`n'+(guardia1==guardmerge`i')*(guardia1==0)
  replace different_`n'=different_`n'+(guardia1+guardmerge`i'==1)
  
 }
 replace guards_`n'=guards_`n'/p_italy
 replace noguards_`n'=noguards_`n'/(1-p_italy)
 foreach p of varlist p_italy /*p_provincia p_comune*/ {
  gen base_same_`p'_`n'=`p'^2+(1-`p')^2
  gen base_guards_`p'_`n'=`p'^2
  gen base_noguards_`p'_`n'=(1-`p')^2
  gen base_different_`p'_`n'=1-base_same_`p'_`n'
  
  replace base_guards_`p'_`n'=base_guards_`p'_`n'/`p'
  replace base_noguards_`p'_`n'=base_noguards_`p'_`n'/(1-`p')
  
  foreach x in "same" "guards" "noguards" "different" {
   egen base_`x'_`p'_`n'_mean=mean(base_`x'_`p'_`n'), by(year)
  }
 }
 
 foreach x in "same" "guards" "noguards" "different" {
  replace `x'_`n'=`x'_`n'/`n'
  egen `x'_`n'_mean=mean(`x'_`n'), by(year)
  
  gen `x'_`n'_D=`x'_`n'_mean-base_`x'_p_italy_`n'_mean
/*  sort year
  twoway line `x'_`n'_mean year || line base_`x'_p_italy_`n'_mean year || line `x'_`n'_D year /*|| line base_`x'_p_provincia_`n'_mean  year|| line base_`x'_p_comune_`n'_mean year*/
  graph export "coord_`x'_`n'_neighbors.pdf", replace */
 }
}


label var same_1_mean "Observed"
label var base_same_p_italy_1_mean "Random Baseline"
sort year
twoway line guards_1_mean noguards_1_mean different_1_mean  year, scheme(s1mono) text(0.86 2003 "Neither Guarded") text(0.28 2006 "Both Guarded") text(0.11 2003 "One Guarded") ylabel(0(0.2)1) legend(off) xtitle("")
graph export "Figures/guard_breakdown.pdf", replace

label var same_1_mean "Observed"
label var base_same_p_italy_1_mean "Random Baseline"
sort year
twoway line same_1_mean base_same_p_italy_1_mean year, scheme(s1mono) xtitle("") text(0.89 2007 "Observed") text(0.83 2007 "Random Baseline") legend(off) 
graph export "Figures/Same_1.pdf", replace

label var guards_1_mean "Observed"
label var base_guards_p_italy_1_mean "Random Baseline"
twoway line guards_1_mean base_guards_p_italy_1_mean year, scheme(s1mono) xtitle("") text(0.25 2006 "Observed") text(0.12 2006 "Random Baseline") legend(off) ylabel(0(0.1)0.5)
graph export "Figures/Guards_1.pdf", replace



/* Summary stats bar graph over time */
use "Data/proprietary/01 Master Data", clear
keep year guardia1
egen tot_guards=sum(guardia1), by(year)
egen tot_banks=count(guardia1), by(year)
duplicates drop year tot_guards tot_banks, force
drop if year==2010
replace tot_banks=tot_banks-tot_guards
label var tot_banks "Total Banks"
label var tot_guards "Banks with Guards"
graph bar tot_guards tot_banks, over(year) stack scheme(s1mono) legend(lab(1 "Banks with Guards") lab(2 "Total Banks") order(2 1))
graph export "Figures/guards_over_time_summary.pdf", replace


/** Graph of Italian/US bank robberies over time */

use "Data/non-proprietary/time_series_IT_USA.dta", clear

line us italy year if year<=2018, xlabel(1990(5)2015) xti(Year) scheme(s1mono) text(7800 2006 "US") text(3800 2005 "Italy") legend(off) xtitle("")
graph export "Figures/timeseries.pdf", replace





/*** ROBUSTNESS PICTURES FOR REGRESSIONS 



local tot0=5
local tot1=6

use "Data/proprietary/01 Master Data", replace
drop if year==2010
gen chara1=nvisible1+ninvisible1
sort chiave_agenzia1 year

gen glat=int(latitude/0.01/50)
gen glong=int(longitude/0.01/50)
bysort glat glong year: gen x=_n==1
gen cluster50=sum(x)
drop x glat glong
local cluster "cluster50"
tempfile temp
save `temp', replace

qui {
forval n=`tot0'/`tot1' {
 use `temp', clear
 gen glat=int(latitude/0.01/`n')
 gen glong=int(longitude/0.01/`n')

 bysort glat glong: gen x=_n==1
 gen ggroup`n'=sum(x)
 drop x
 
  sort chiave_agenzia1 year
 gen year2=year^2

 qui egen guard_tot=sum(guardia1), by(ggroup`n' year)
 qui egen bank_tot=count(ggroup`n'), by(ggroup`n' year)

 qui gen guardia01=((guard_tot-guardia1)/(bank_tot-1)) 
 qui replace guardia01=0 if guardia01==.
 qui gen guardia11=((guard_tot-guardia1)/(bank_tot-1))*(guardia1)
 qui gen no_substitutes=guardia11==.
 qui replace guardia11=0 if guardia11==.

  reghdfe attempted1 guardia1 guardia01 guardia11 no_substitutes, /*
 */ absorb(ggroup`n'#c.year /*ggroup`n'#c.year2*/ year) cluster(`cluster')
 
 local d0_`n'=_b[guardia1]
 local d0lo_`n'=_b[guardia1]-1.64*_se[guardia1]
 local d0hi_`n'=_b[guardia1]+1.64*_se[guardia1]
 lincom guardia11+guardia1-guardia01
 local d1_`n'=r(estimate)
 local d1lo_`n'=r(estimate)-1.64*r(se)
 local d1hi_`n'=r(estimate)+1.64*r(se)
 local dd0_`n'=_b[guardia01]
 local dd0lo_`n'=_b[guardia01]-1.64*_se[guardia01]
 local dd0hi_`n'=_b[guardia01]+1.64*_se[guardia01]
 local dd1_`n'=_b[guardia11]
 local dd1lo_`n'=_b[guardia11]-1.64*_se[guardia11]
 local dd1hi_`n'=_b[guardia11]+1.64*_se[guardia11]
}

foreach stub in "d0" "d1" "dd0" "dd1" {
 gen `stub'=.
 gen `stub'lo=.
 gen `stub'hi=.

 forval j=1/1 {
  replace `stub'=``stub'_`j'' in `j'
  replace `stub'lo=``stub'lo_`j'' in `j'
  replace `stub'hi=``stub'hi_`j'' in `j'
 }
}

}
drop if _n>1
gen n=_n
gen z=0
label var d0 "D{sub:0}"
label var d1 "D{sub:1}"
label var dd0 "{&Delta}{sub:0}"
label var dd1 "{&Delta}{sub:1}"

twoway rarea d0hi d0lo n, fc(gs13) lc(gs13) || line d0 n, lc(gs5) || line z n, scheme(s1mono) xtitle("Market Size (km)") ytitle("D{sub:0}") legend(off)
graph export "Figures/d0.pdf", replace

twoway rarea d1hi d1lo n, fc(gs13) lc(gs13) || line d1 n, lc(gs5) || line z n, scheme(s1mono) xtitle("Market Size (km)") ytitle("D{sub:1}") legend(off)
graph export "Figures/d1.pdf", replace

twoway rarea dd0hi dd0lo n, fc(gs13) lc(gs13) || line dd0 n, lc(gs5) || line z n, scheme(s1mono) xtitle("Market Size (km)") ytitle("{&Delta}{sub:0}") legend(off)
graph export "Figures/dd0.pdf", replace

twoway rarea dd1hi dd1lo n, fc(gs13) lc(gs13) || line dd1 n, lc(gs5) || line z n, scheme(s1mono) xtitle("Market Size (km)") ytitle("{&Delta}{sub:1}") legend(off)
graph export "Figures/dd1.pdf", replace


/** MAPS **

scatter latitude longitude, msym(o) yline(35.5(.5)47,lwidth( vvthin )) xline(6.7(.5)18.5,lwidth( vvthin )) xlab(6.7(3)18.5) ylab(35.5(3)47)
graph export "Figures/italy_grid1.pdf", replace

scatter latitude longitude, msym(o) yline(35.5(.05)47,lwidth( vvthin )) xline(6.7(.05)18.5,lwidth( vvthin )) xlab(6.7(3)18.5) ylab(35.5(3)47)
graph export "Figures/italy_grid2.pdf", replace
